package com.kingyun.gpsinspection.purificationservice.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.DutyRecordsMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.DutyRecords;
import com.kingyun.gpsinspection.purificationservice.facade.model.DutyRecordsVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.DutyRecordsService;
import com.kingyun.gpsinspection.purificationservice.facade.service.activiti.base.WorkFlowService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jiangjx on 2017/10/25.
 */
public class DutyRecordsServiceImpl implements DutyRecordsService {

    @Autowired
    private DutyRecordsMapper dutyRecordsMapper;
    @Autowired
    private WorkFlowService workFlowService;

    /**
     * 查询列表数据
     *
     * @param dutyRecordsVo
     * @return
     */
    @Override
    public JSONObject queryTableInfo(DutyRecordsVo dutyRecordsVo) {
        // 返回结果集
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        dutyRecordsVo.setLimit(dutyRecordsVo.getLimit() == null ? Integer.valueOf(10) : dutyRecordsVo.getLimit());
        dutyRecordsVo.setOffset(dutyRecordsVo.getOffset() == null ? Integer.valueOf(0) : dutyRecordsVo.getOffset());
        // 总条数
        jsonObject.put("total", dutyRecordsMapper.queryCount(dutyRecordsVo));
        // 信息
        List<DutyRecords> list = dutyRecordsMapper.queryList(dutyRecordsVo);
        List<DutyRecordsVo> vos = new ArrayList<>();
        list.stream().filter(dutyRecords -> null != dutyRecords && null != dutyRecords.getStartTime() && null != dutyRecords.getFinishTime()).forEach(dutyRecords -> {
            DutyRecordsVo vo = new DutyRecordsVo();
            BeanUtils.copyProperties(dutyRecords, vo);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            vo.setOnDutyDate(dateFormat.format(vo.getStartTime()) + " 至 " + dateFormat.format(vo.getFinishTime()));
            vos.add(vo);
        });
        jsonObject.put("rows", JSON.toJSON(vos));
        return jsonObject;
    }

    /**
     * 保存干部值班记录
     *
     * @param dutyRecords
     * @param logginUser
     * @return
     */
    @Override
    public Integer saveInfo(DutyRecords dutyRecords, EmployeeVo logginUser, String lhcxRadio, String riskRadio) {
        dutyRecords.setId(UniqueUtil.uuid());
        dutyRecords.setOnDutyId(logginUser.getEmployeeId());
        formatData(dutyRecords, lhcxRadio, riskRadio);
        return dutyRecordsMapper.insertSelective(dutyRecords);
    }

    /**
     * 初始化值班数据
     */
    private void formatData(DutyRecords dutyRecords, String lhcxRadio, String riskRadio) {
        //主体装置-脱硫单元溶液循环量、循环泵运行是否正常
        dutyRecords.setVal1(StringUtils.isEmpty(dutyRecords.getVal1()) ? "正常" : dutyRecords.getVal1());
        //主体装置-脱硫单元再生塔顶温度、贫液质量是否正常
        dutyRecords.setVal2(StringUtils.isEmpty(dutyRecords.getVal2()) ? "正常" : dutyRecords.getVal2());
        //主体装置-脱水单元溶液循环、循环泵运行是否正常
        dutyRecords.setVal3(StringUtils.isEmpty(dutyRecords.getVal3()) ? "正常" : dutyRecords.getVal3());
        //主体装置-脱水单元重浮器温度、贫液质量是否正常
        dutyRecords.setVal4(StringUtils.isEmpty(dutyRecords.getVal4()) ? "正常" : dutyRecords.getVal4());
        //主体装置-硫磺回收单元主燃烧炉、尾气灼烧炉温度是否正常
        dutyRecords.setVal6(StringUtils.isEmpty(dutyRecords.getVal6()) ? "正常" : dutyRecords.getVal6());
        //主体装置-硫磺回收单元回压、风机运行是否正常
        dutyRecords.setVal7(StringUtils.isEmpty(dutyRecords.getVal7()) ? "正常" : dutyRecords.getVal7());
        //主体装置-尾气是否达标排放
        dutyRecords.setVal8(StringUtils.isEmpty(dutyRecords.getVal8()) ? "正常" : dutyRecords.getVal8());
        //辅助装置-消防设施是否正常
        dutyRecords.setVal9(StringUtils.isEmpty(dutyRecords.getVal9()) ? "正常" : dutyRecords.getVal9());
        //辅助装置-污水处理设备设施是否正常
        dutyRecords.setVal10(StringUtils.isEmpty(dutyRecords.getVal10()) ? "正常" : dutyRecords.getVal10());
        //辅助装置-放空燃料是否正常
        dutyRecords.setVal12(StringUtils.isEmpty(dutyRecords.getVal12()) ? "正常" : dutyRecords.getVal12());
        //公共工程系统-循环水系统压力、泵运行是否正常
        dutyRecords.setVal13(StringUtils.isEmpty(dutyRecords.getVal13()) ? "正常" : dutyRecords.getVal13());
        //公共工程系统-蒸汽系统压力、锅炉运行是否正常
        dutyRecords.setVal14(StringUtils.isEmpty(dutyRecords.getVal14()) ? "正常" : dutyRecords.getVal14());
        //公共工程系统-仪表风压力、空压机运行是否正常
        dutyRecords.setVal15(StringUtils.isEmpty(dutyRecords.getVal15()) ? "正常" : dutyRecords.getVal15());
        //公共工程系统-燃料气系统压力是否正常
        dutyRecords.setVal16(StringUtils.isEmpty(dutyRecords.getVal16()) ? "正常" : dutyRecords.getVal16());
        if (!StringUtils.isEmpty(riskRadio) && riskRadio.equals("1")) {
            //风险作业-是否有检维修作业、项目施工作业
            dutyRecords.setVal23(StringUtils.isEmpty(dutyRecords.getVal23()) ? "正常" : dutyRecords.getVal23());
            //风险作业-是否办理作业许可票证
            dutyRecords.setVal24(StringUtils.isEmpty(dutyRecords.getVal24()) ? "正常" : dutyRecords.getVal24());
            //风险作业-属地监督是否在场
            dutyRecords.setVal25(StringUtils.isEmpty(dutyRecords.getVal25()) ? "正常" : dutyRecords.getVal25());
            //风险作业-作业人员劳保措施是否到位
            dutyRecords.setVal28(StringUtils.isEmpty(dutyRecords.getVal28()) ? "正常" : dutyRecords.getVal28());
            //风险作业-是否按规范作业
            dutyRecords.setVal29(StringUtils.isEmpty(dutyRecords.getVal29()) ? "正常" : dutyRecords.getVal29());
        }
        if (!StringUtils.isEmpty(lhcxRadio) && lhcxRadio.equals("1")) {
            //硫磺成型生产-是否有硫磺生产作业、硫磺装车作业
            dutyRecords.setVal17(StringUtils.isEmpty(dutyRecords.getVal17()) ? "正常" : dutyRecords.getVal17());
            //硫磺成型生产-作业人员劳保措施是否到位
            dutyRecords.setVal19(StringUtils.isEmpty(dutyRecords.getVal19()) ? "正常" : dutyRecords.getVal19());
            //硫磺成型生产-计量抽检是否按规划进行
            dutyRecords.setVal20(StringUtils.isEmpty(dutyRecords.getVal20()) ? "正常" : dutyRecords.getVal20());
            //硫磺成型生产-硫磺堆码是否符合规定
            dutyRecords.setVal21(StringUtils.isEmpty(dutyRecords.getVal21()) ? "正常" : dutyRecords.getVal21());
            //硫磺成型生产-车辆是否配戴防火罩
            dutyRecords.setVal22(StringUtils.isEmpty(dutyRecords.getVal22()) ? "正常" : dutyRecords.getVal22());
        }
    }

    /**
     * 修改干部值班信息
     *
     * @param dutyRecords
     */
    @Override
    public Integer updateInfo(DutyRecords dutyRecords, String lhcxRadio, String pageType, EmployeeVo employeeVo, String riskRadio) {
        if (null != dutyRecords && !StringUtils.isEmpty(dutyRecords.getId())) {
            if (!StringUtils.isEmpty(pageType) && pageType.equals("add")) {
                formatData(dutyRecords, lhcxRadio, riskRadio);
                //结束流程
                if (!StringUtils.isEmpty(dutyRecords.getTaskId())) {
                    workFlowService.completeTask(dutyRecords.getTaskId(), null, null, dutyRecords.getOnDutyId());
                }
            }
            dutyRecords.setModifyUser(employeeVo.getEmployeeId());
            dutyRecords.setModifyTime(new Date());
            return dutyRecordsMapper.updateByPrimaryKeySelective(dutyRecords);
        }
        return 0;
    }

    /**
     * 删除干部值班信息
     *
     * @param id
     * @return
     */
    @Override
    public Integer delInfo(String id) {
        if (!StringUtils.isEmpty(id)) {
            return dutyRecordsMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    /**
     * 根据id查询值班信息
     *
     * @param id
     */
    @Override
    public DutyRecords queryInfoById(String id) {
        return dutyRecordsMapper.selectByPrimaryKey(id);
    }

    /**
     * 保存多条信息
     *
     * @param dutyItems
     */
    @Override
    public int saveListInfo(String dutyItems, EmployeeVo employeeVo) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        List<DutyRecords> list = new ArrayList<>();
        JSONArray jsonArray = JSON.parseArray(dutyItems);
        //将页面传上来的array数据转换为list
        for (Object o : jsonArray) {
            DutyRecords dutyRecords = new DutyRecords();
            JSONObject jsonObject = (JSONObject) o;
            dutyRecords.setId(UniqueUtil.uuid());
            dutyRecords.setStartTime(simpleDateFormat.parse(jsonObject.get("startDate").toString()));
            dutyRecords.setFinishTime(simpleDateFormat.parse(jsonObject.get("endDate").toString()));
            dutyRecords.setOnDutyId(jsonObject.get("dutyId").toString());
            dutyRecords.setOnDutyName(jsonObject.get("dutyName").toString());
            dutyRecords.setIsCreate(0);
            dutyRecords.setCreateTime(new Date());
            dutyRecords.setCreateUser(employeeVo.getEmployeeId());
            dutyRecords.setModifyTime(new Date());
            dutyRecords.setModifyUser(employeeVo.getEmployeeId());
            list.add(dutyRecords);
        }
        return dutyRecordsMapper.insertSelectives(list);
    }
}
